package com.mashibing.singleton;

/**
 * 懒汉式，双重判断可以保证线程安全
 * @author: Yuanzt
 * @Date: 2021/3/8
 */
public class Mgr06 {

    private static volatile Mgr06 INSTANCE;

    private Mgr06(){}

    public static Mgr06 getInstance(){
        if (INSTANCE == null){
            //双重检查
            synchronized (Mgr06.class){
                if (INSTANCE == null){
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    INSTANCE = new Mgr06();
                }
            }
        }
        return INSTANCE;
    }

    public static void main(String[] args) {
        for (int i=0;i<100;i++){
            new Thread(()-> System.out.println(Mgr06.getInstance().hashCode())).start();
        }
    }
}
